//
//  WelcomeViewController.swift
//  SinaBlog
//
//  Created by 有何不可 on 16/1/4.
//  Copyright © 2016年 有何不可. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    var bottomConstraint: Constraint?
    
    override func loadView() {
        super.loadView()
        
        view = imageView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setUI()
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
//        headerView.snp_makeConstraints { (make) -> Void in
//            bottomConstraint = make.bottom.equalTo(view.snp_bottom).offset(150 - UIScreen.mainScreen().bounds.size.height).constraint
//        }
//        
        bottomConstraint?.updateOffset(150 - UIScreen.mainScreen().bounds.size.height)
        
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            //当修改自动布局 就要重新刷新
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                //在动画执行2秒钟后,发送这个通知,让Appdelegate接受通知,执行跳转控制器的方法!~
                NSNotificationCenter.defaultCenter().postNotificationName(key, object: self)
        }
        
        
    }

    private func setUI() {
        
        view.addSubview(headerView)
        
        
        headerView.sd_setImageWithURL(NSURL(string: OauthViewModel.sharedInstance.model?.profile_image_url ?? ""))
        
        
        view.addSubview(textLal)
        
        headerView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            
          bottomConstraint = make.bottom.equalTo(view.snp_bottom).offset(-150).constraint
        }
        
        textLal.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(headerView.snp_centerX)
            
            make.top.equalTo(headerView.snp_bottom).offset(10)
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    private lazy var imageView: UIImageView = {
        
        let imgView = UIImageView(image: UIImage(named: "ad_background"))
        
       return imgView
    }()
    
    private lazy var headerView: UIImageView = {
        
       let imgView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        imgView.layer.cornerRadius = 25
        
        imgView.clipsToBounds = true
        
        return imgView
    }()
    
    private lazy var textLal: UILabel = {
        
       let textLal = UILabel()
        
        textLal.text = "欢迎归来"
        
        textLal.font = UIFont.systemFontOfSize(12)
        
        textLal.textColor = UIColor.darkGrayColor()
        
        return textLal
    }()
}
